package com.styytech.dadabao.ui.more;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.lidroid.xutils.view.annotation.ContentView;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.constant.ConstantsCommon;
import com.styytech.dadabao.constant.ConstantsServerUrl;
/**
 * 服务公告
 */
@ContentView(R.layout.activity_notice_list)
public class NoticeFragmentActivity extends BaseActivity {

    /*上下文*/
    private Context mContext;
    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    /*服务公告列表*/
    private NoticeListFragment noticeListFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    /**
     * 初始数据
     */
    private void initData() {
        mContext = this;
        fragmentManager = getSupportFragmentManager();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setTopBar("服务公告", View.VISIBLE);
        initFragment();
    }

    /**
     * 初始化fragment
     */
    private void initFragment() {

        transaction = fragmentManager.beginTransaction();
        noticeListFragment = (NoticeListFragment) fragmentManager
                .findFragmentByTag("ID_NOTICE");
        if (noticeListFragment == null) {
            // 如果noticeListFragment为空，则创建一个并添加到界面上
            String newsUrl = ConstantsServerUrl.getNoticeListUrl();
            noticeListFragment = NoticeListFragment.newInstance(newsUrl,
                    ConstantsCommon.DATA_SRC_NOTICE,
                    ConstantsCommon.ADAPTER_TYPE_NOTICE, -1);
            transaction.add(R.id.fl_content, noticeListFragment,"ID_NOTICE");
        } else {
            // 如果MessageFragment不为空，则直接将它显示出来
            transaction.show(noticeListFragment);
        }
        transaction.commit();
    }
}
